package org.clienttest;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.imageio.ImageIO;

public class Font2png {
    public static Font loadFont(String fontFileName, float fontSize) // 第一个参数是外部字体名，第二个是字体大小
    {
        try {
            File file = new File(fontFileName);
            if(!file.exists()){
                System.out.println("找不到字体文件");
            }
             
            FileInputStream aixing = new FileInputStream(file);
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
            Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
            aixing.close();
            return dynamicFontPt;
        } catch (Exception e)// 异常处理
        {http://www.huiyi8.com/gundongdaima/html/
            e.printStackTrace();
            return new java.awt.Font("宋体", Font.PLAIN, (int)fontSize);
        }
    }

    public static void main(String[] args) {
        float fontSize=50;
        int color=0xff000000;
        String out=new File("C:/").getAbsolutePath();
        String fontPath= "font/fontawesome-webfont.ttf";
        System.out.println(fontPath);
        String name="icon.png";
        String text="\uF02c";
        int paddind=2;
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
//        try {
//            args=br.readLine().split("\\s+");
//        } catch (IOException e1) {
//            e1.printStackTrace();
//        }
         
        if(args!=null){
            for (int i = 0; i < args.length; i+=2) {
                if(i==args.length-1){
                    continue;
                }
                String key=args[i];
                String value=args[i+1];
                if("--name".equals(key)){
                    name=value;
                }else if("--text".equals(key)){
                    text=value;
                }if("--padding".equals(key)){
                    paddind=Integer.parseInt(value);
                }else if("--fontpath".equals(key)){
                    fontPath=value;
                }else if ("--out".equals(key)) {
                    out=value;
                }else if("--size".equals(key)){
                    fontSize=Float.valueOf(value);
                }else if ("--color".equals(key)) {
                    color=Integer.decode(value);
                }
            }
        }
        int imgSize=(int) (paddind*2+fontSize);
        BufferedImage image = new BufferedImage(imgSize, imgSize,
                BufferedImage.TYPE_4BYTE_ABGR_PRE);
        Graphics g=image.getGraphics();
        Font font=loadFont(fontPath, fontSize);
        g.setFont(font);
        g.setColor(new Color(color,true));

        FontMetrics fm = g.getFontMetrics();
        int stringWidth = fm.stringWidth(text);
        int stringAscent = fm.getAscent();
        int stringDescent = fm.getDescent ();
        int x = image.getWidth() / 2 - stringWidth / 2;
        int y = image.getHeight() / 2 + (stringAscent - stringDescent) / 2;
         
        g.drawString(text, x, y);
         
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(out+'/'+name);
            ImageIO.write(image, "png", fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}